package com.neutech.maplestory.entity;

import com.neutech.maplestory.client.MapleStoryClient;
import com.neutech.maplestory.util.ImageUtil;

import java.awt.*;

public class Mob extends AbstractMapleStoryObject{
   public static Image[] imgs = new Image[100];
   static {
       for (int i = 0; i < 8; i++) {
           imgs[i] = ImageUtil.getImage("mob01_left_stand" + i);
       }
           for (int i = 8; i < 16; i++) {
               imgs[i] = ImageUtil.getImage("mob01_left_walk" + (i - 8));
           }
       for (int i = 16; i < 37; i++) {
           imgs[i] = ImageUtil.getImage("mob01_left_die" + (i - 16));
       }

       }

    /**
     * 最大血量
     */
    public int MAX_HP;
    public Mob(){
       this.width = imgs[0].getWidth(null);
       this.height = imgs[0].getHeight(null);
       this.dir = Direction.LEFT;
       this.action = Action.STAND;
       this.live = true;
       this.MAX_HP = 100;
       this.HP = MAX_HP;


    }
    public Mob(MapleStoryClient msc,int x,int y,int speed){
       this();
       this.msc = msc;
       this.x = x;
       this.y = y;
       this.speed = speed;
   }
    private int count;
   private  int step;
   private int dieStep;

    @Override
    public void draw(Graphics g) {
        if (dieStep >= 20){
           this.live = false;
           dieStep = 0;
        }
        if (!live) {
            msc.mobs.remove(this);
            return;
        }
      switch (dir){
          case LEFT:
              switch (action){
                  case STAND:
                      if(count++ % 3 ==0 ){
                          g.drawImage(imgs[step++ % 8],x,y,null);
                      }else{
                          g.drawImage(imgs[step % 8],x,y,null);
                      }
                      break;
                  case WALK:
                      if(count++ % 3 ==0 ){
                          g.drawImage(imgs[step++ % 8 + 8],x,y,null);
                      }else{
                          g.drawImage(imgs[step % 8 + 8],x,y,null);
                      }
                      break;
                  case DIE:
                      if(count++ % 3 ==0 ){
                          g.drawImage(imgs[dieStep++ % 21 + 16],x,y,null);
                      }else{
                          g.drawImage(imgs[dieStep % 21 + 16],x,y,null);
                      }
                      break;
                  default:
                      break;
              }
              break;
          default:
              break;
      }
        if(HP < MAX_HP) {
            blooBar.draw(g);
        }
      move();
    }

    @Override
    public void move() {
        switch (dir){
            case LEFT:
               switch (action){
                   case WALK:
                       this.x -= speed;
                       break;
                   default:
                       break;
               }
                break;
            case RIGHT:
                this.x += speed;
                break;
            default:
                break;
        }
    }
    public BlooBar blooBar = new BlooBar();
    class BlooBar extends AbstractMapleStoryObject{
        private Image img;
        public BlooBar(){
            this.img = ImageUtil.getImage("blood");
            this.width = img.getWidth(null);
        }

        @Override
        public void draw(Graphics g) {
//            //框
//            //内部类访问外部类相同属性名时，使用外部类.this.属性名
//            Color c =g.getColor();
//            g.drawRect(Mob.this.x,Mob.this.y - 10, Mob.this.width,10);
//            //填充
//            g.setColor(Color.GREEN);
//            g.fillRect(Mob.this.x + 1,Mob.this.y - 10 + 1, (int)(Mob.this.width * Mob.this.HP / Mob.this.MAX_HP),10 - 1);
//            g.setColor(c);
            int n = Mob.this.width/this.width * Mob.this.HP / Mob.this.MAX_HP;
            for (int i = 0; i < n; i++) {
                g.drawImage(img,Mob.this.x + (i * this.width),Mob.this.y - 10,null);
            }

        }
    }
}
